package cn.ubeta.b2c.common;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import cn.ubeta.b2c.R;
import cn.ubeta.b2c.user.LoginActivity;
import cn.ubeta.b2c.user.OrderActivity;
import cn.ubeta.b2c.utils.AppConfig;
import cn.ubeta.b2c.utils.Constants;
import cn.ubeta.b2c.utils.TitleBarHelper;

/**
 * Created by liudengyong on 16/8/28.
 */

public class AnalyseActivity extends AppCompatActivity {

    protected String getPageName() {
        return null;
    }

    protected boolean needPermissionCheck() {
        return true;
    }

    protected void onCreate(Bundle savedInstanceState, int layout) {
        super.onCreate(savedInstanceState);
        if (layout != 0) {
            setContentView(layout);
            // 设置标题栏返回键
            TitleBarHelper.setBack(this);
        }
        // 第三方统计PV统计
        if (Constants.OPEN_ANALYSE) {
            // TODO 第三方统计
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        onCreate(savedInstanceState, 0);

        // 统计页面
        if (!TextUtils.isEmpty(getPageName())) {
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        if (needPermissionCheck())
            checkAppPermission();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 统计页面
        if (!TextUtils.isEmpty(getPageName())) {
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 跳转订单
        if (AppConfig.isPending2Order()) {
            OrderActivity.gotoActivityWithLogin(this, OrderActivity.class, false);
            AppConfig.cleanPending2Order();
        }
    }

    /**
     * 跳转页面,带参数
     */
    public static void gotoActivity(Context context, Class clazz, Bundle extras) {
        gotoActivity(context, false, clazz, false, extras);
    }

    /**
     * 跳转页面
     */
    public static void gotoActivity(Context context, Class clazz) {
        gotoActivity(context, false, clazz, false, null);
    }

    /**
     * 跳转页面, 带动画, 带数据
     */
    public static void gotoActivityWithLogin(Context context, Class clazz, Bundle extras) {
        gotoActivity(context, true, clazz, true, extras);
    }

    /**
     * 跳转页面, 是否带动画
     */
    public static void gotoActivityWithLogin(Context context, Class clazz, boolean leftRightAnim) {
        gotoActivity(context, true, clazz, leftRightAnim, null);
    }

    /**
     * 跳转页面, 是否带动画
     */
    public static void gotoActivity(Context context, Class clazz, boolean leftRightAnim) {
        gotoActivity(context, false, clazz, leftRightAnim, null);
    }

    /**
     * 跳转页面, 是否检查登录, 是否带动画
     */
    private static void gotoActivity(Context context, boolean withLogin, Class clazz, boolean leftRightAnim, Bundle extras) {
        Intent intent = new Intent();
        if (extras != null)
            intent.putExtras(extras);
        if (withLogin && !LoginCommon.isLogin()) {
            intent.setClass(context, LoginActivity.class);
            intent.putExtra(LoginActivity.PARAMS_CB_ACTIVITY, clazz.getCanonicalName());
            if (extras != null)
                intent.putExtra(LoginActivity.PARAMS_CB_EXTRAS, extras);
            leftRightAnim = true; // 登录必弹动画
        } else {
            intent.setClass(context, clazz);
        }
        context.startActivity(intent);
        if (leftRightAnim && context instanceof Activity) {
            ((Activity)context).overridePendingTransition(R.anim.in_right_to_left, android.R.anim.fade_out);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
         if (requestCode == Constants.REQ_CODE_PERMISSION) {
            // TODO 获取权限后
        }
    }

    /**
     * 检查权限
     */
    protected boolean checkAppPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            // 请求权限
            ActivityCompat.requestPermissions(this, new String[]{
                            Manifest.permission.READ_PHONE_STATE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    Constants.REQ_CODE_PERMISSION);
            return false;
        }
        return true;
    }
}
